import java.util.Scanner;

public class Three {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt(),i,k;
		String[] str=new String[n];
		String s1,s2;
		for (i = 0; i < n; i++) 
			str[i]=scan.next();
		for (i = 0; i < str.length-1; i++){
			for (k = 0; k < str.length-1-i; k++){
				s1=str[k].substring(6, 14);
				s2=str[k+1].substring(6, 14);
				if(Integer.parseInt(s1)>Integer.parseInt(s2)){
					s1=str[k];
					str[k]=str[k+1];
					str[k+1]=s1;
				}
			}
		}
		while(scan.hasNextLine()){
			s1=scan.next();
			if(s1.equals("sort1")){
				for (k = 0; k < str.length; k++){
					System.out.println(str[k].substring(6, 10)+"-"+str[k].substring(10, 12)+"-"+str[k].substring(12, 14));
				}
			}
			if(s1.equals("sort2")){
				for (k = 0; k < str.length; k++)
					System.out.println(str[k]);
			}
			if(!s1.equals("sort1")&&!s1.equals("sort2"))
				System.out.println("exit");
		}
		scan.close();
	}
	
}
